package com.tcy.game.sj;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;

/**
 * 分数排行
 * Created by 80002023 on 2016/7/28.
 */
public class HighscoresScreen implements Screen {
    Game game;

    OrthographicCamera guiCam;
    SpriteBatch spriteBatch;
    Rectangle backBounds;
    Vector3 touchPoint;
    String[] highScores;
    float xOffset = 0;
    GlyphLayout glyphLayout;

    public HighscoresScreen(Game game) {
        this.game = game;

        guiCam = new OrthographicCamera(320, 480);
        guiCam.position.set(320 / 2, 480 / 2, 0);
        backBounds = new Rectangle(0, 0, 64, 64);
        touchPoint = new Vector3();
        spriteBatch = new SpriteBatch();
        highScores = new String[5];

        glyphLayout = new GlyphLayout();
        for (int i = 0; i < 5; i++) {
            highScores[i] = i + 1 + ". " + Settings.highscores[i];
            glyphLayout.setText(Assets.font, highScores[i]);
            xOffset = Math.max(glyphLayout.width, xOffset);
        }
        xOffset = 160 - xOffset / 2 + Assets.font.getSpaceWidth() / 2;
    }

    public void update(float deltaTime) {
        if (Gdx.input.justTouched()) {
            guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

            if (OverlapTester.pointInRectangle(backBounds, touchPoint.x, touchPoint.y)) {
                Assets.playSound(Assets.clickSound);
                game.setScreen(new MainMenu(game));
                return;
            }
        }
    }

    public void draw(float deltaTime) {
        GL20 gl = Gdx.gl;
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        guiCam.update();

        spriteBatch.setProjectionMatrix(guiCam.combined);
        spriteBatch.disableBlending();
        spriteBatch.begin();
        spriteBatch.draw(Assets.backgroundRegion, 0, 0, 320, 480);
        spriteBatch.end();

        spriteBatch.enableBlending();
        spriteBatch.begin();
        spriteBatch.draw(Assets.highScoresRegion, 10, 360 - 16, 300, 33);

        float y = 230;
        for (int i = 4; i >= 0; i--) {
            Assets.font.draw(spriteBatch, highScores[i], xOffset, y);
            y += Assets.font.getLineHeight();
        }

        spriteBatch.draw(Assets.arrow, 0, 0, 64, 64);
        spriteBatch.end();
    }

    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        update(delta);
        draw(delta);
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }
}
